//
//  Controller.mm
//
//  Created by Michael Barriault on 10-11-06.
//  Copyright 2010 MikBarr Studios. All rights reserved.
//

#import "Controller.h"
#include <string>
#include <sstream>

@implementation Controller
-(void)awakeFromNib {
	isRunning = FALSE;
	Badge = new CommCocoa;
	Badge->statusField = statusBox;
	Badge->progressBar = progressBar;
	std::vector<std::string> argv;
	MW T(Badge,argv);
	[nField setIntValue:T.n];
	[TField setDoubleValue:T.T];
	[sField setDoubleValue:T.s];
	[eField setDoubleValue:T.epsilon];
	[kField setDoubleValue:T.K];
	[jField setDoubleValue:T.J];
	[p0Field selectCellWithTag:T.p(0)];
	[p1Field selectCellWithTag:T.p(1)];
	[p2Field selectCellWithTag:T.p(2)];
	[iField setStringValue:[NSString stringWithCString:T.id.c_str() encoding:NSASCIIStringEncoding]];
}
-(void)dealloc {
	Badge->release();
	[super dealloc];
}
- (IBAction)Run:(id)sender {
    [self performSelectorInBackground:@selector(RunThread) withObject:nil];
}
-(id)RunThread {
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	if (!isRunning) {
		isRunning = TRUE;
		[progressBar startAnimation:self];
		[goButton setTitle:@"Stop!"];
		std::vector<std::string> argv;
		argv.push_back((string)"n" + itos([nField intValue]));
		argv.push_back((string)"T" + ftos([TField doubleValue]));
		argv.push_back((string)"s" + ftos([sField doubleValue]));
		argv.push_back((string)"e" + ftos([eField doubleValue]));
		argv.push_back((string)"k" + ftos([kField doubleValue]));
		argv.push_back((string)"j" + ftos([jField doubleValue]));
		argv.push_back((string)"p" + itos([[p0Field selectedCell] tag]) + itos([[p1Field selectedCell] tag]) + itos([[p2Field selectedCell] tag]));
		if ( [[iField stringValue] length] > 0 ) {
			argv.push_back((string)"i");
			argv.push_back((string)[[iField stringValue] cStringUsingEncoding:NSASCIIStringEncoding]);
		}
		S = MW(Badge, argv);
		S.Solve();
		[progressBar stopAnimation:self];
	}
	else {
		isRunning = FALSE;
		S.stopit = true;
	}
	[goButton setTitle:@"Go!"];
	[pool release];
	return 0;
}
@end
